package com.ruoyi.ruoyiyinzheng.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.satoken.utils.LoginHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.ruoyiyinzheng.domain.vo.StExperimentRequestVo;
import com.ruoyi.ruoyiyinzheng.domain.bo.StExperimentRequestBo;
import com.ruoyi.ruoyiyinzheng.service.IStExperimentRequestService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 实验要求控制器
 * 前端访问路由地址为:/ruoyiyinzheng/experimentRequest
 *
 * @author ruoyi
 * @date 2023-09-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/experimentRequest")
public class StExperimentRequestController extends BaseController {

    private final IStExperimentRequestService iStExperimentRequestService;

    /**
     * 查询实验要求列表
     */
    @SaCheckPermission("ruoyiyinzheng:experimentRequest:list")
    @GetMapping("/list")
    public TableDataInfo<StExperimentRequestVo> list(StExperimentRequestBo bo, PageQuery pageQuery) {
        return iStExperimentRequestService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出实验要求列表
     */
    @SaCheckPermission("ruoyiyinzheng:experimentRequest:export")
    @Log(title = "实验要求", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StExperimentRequestBo bo, HttpServletResponse response) {
        List<StExperimentRequestVo> list = iStExperimentRequestService.queryList(bo);
        ExcelUtil.exportExcel(list, "实验要求", StExperimentRequestVo.class, response);
    }

    /**
     * 获取实验要求详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ruoyiyinzheng:experimentRequest:query")
    @GetMapping("/{id}")
    public R<StExperimentRequestVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iStExperimentRequestService.queryById(id));
    }

    /**
     * 新增实验要求
     */
    @SaCheckPermission("ruoyiyinzheng:experimentRequest:add")
    @Log(title = "实验要求", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StExperimentRequestBo bo) {
        if(iStExperimentRequestService.selectExperimentNumberCount(Long.valueOf(bo.getExperimentNumber()))>0)
        {
            return    R.fail(StringUtils.format("实验编号：{}存在",bo.getExperimentNumber()));

        }
        return toAjax(iStExperimentRequestService.insertByBo(bo));
    }

    /**
     * 修改实验要求
     */
    @SaCheckPermission("ruoyiyinzheng:experimentRequest:edit")
    @Log(title = "实验要求", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StExperimentRequestBo bo) {
        return toAjax(iStExperimentRequestService.updateByBo(bo));
    }

    /**
     * 删除实验要求
     *
     * @param ids 主键串
     */
    @SaCheckPermission("ruoyiyinzheng:experimentRequest:remove")
    @Log(title = "实验要求", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iStExperimentRequestService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @SaCheckPermission("ruoyiyinzheng:experimentRequest:list")
    @GetMapping(value = "/recipes/{Id}")
    public R<List<String>> recipes(@PathVariable("Id") Long Id) {
        return R.ok(iStExperimentRequestService.selectRecipeNameListById(Id));
    }
}
